const rob = nums => {
  const n = nums.length
  if (n === 1) return nums[0]
  if (n === 2) return Math.max(nums[0], nums[1])
  if (n === 3) return Math.max(nums[0] + nums[2], nums[1])
  let p = q = 0
  const getSum = (index, length, arr, sum) => {
    for (let i = index; i < length; i += 2) {
      sum += arr[i]
    }
    return sum
  }

  const res = Math.max(getSum(0, n, nums, p), getSum(1, n, nums, q))
  return res
}

console.log(rob([2, 1, 1, 2]));

// 思路错误